package starlingui.component
{
	import com.imcrl.component.IDisplayList;
	import com.imcrl.component.PageList;
	import com.imcrl.core.ClassFactory;
	import com.imcrl.event.ListItemEvent;
	import com.imcrl.layout.TileLayout;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.textures.Texture;
	
	import com.imcrl.component.itemRender.ListItemRender;
	
	public class ExpandTileList extends Sprite implements IDisplayList
	{
		private var expadHeight:int=250;
		private var expadTime:Number=0.3;
		
		private var _bmd:BitmapData;
		private var _background:Image;
		private var _cache_top:Image;	
		private var _cache_botton:Image;
		
		private var innerList:PageList;
		private var _topList:TopList;
		
		private var layout:AdvLayout;
		public function ExpandTileList(classFactory:ClassFactory,itemW:int,itemH:int,col:int=1)
		{
			
			layout=new AdvLayout(itemW,itemH,col);
			layout.pad.left=15;
			layout.pad.top=15;
			
			innerList=new PageList(classFactory);
			innerList.allowRepeat=true;
			innerList.layout=layout;
			innerList.addEventListener(ListItemEvent.SELECTED,selectedHandle);
			
			this.addChild(innerList);
		}
		
		public function set backgournd(bitmapdata:BitmapData):void{
			_bmd=bitmapdata;
			var texture:Texture=Texture.fromBitmapData(_bmd);
			_background=new Image(texture);
			_cache_top=new Image(texture);
			_cache_botton=new Image(texture);
			_topList=new TopList();
			
			_cache_top.addEventListener(TouchEvent.TOUCH,hideHandle);
			_cache_botton.addEventListener(TouchEvent.TOUCH,hideHandle);
			
			this.addChildAt(_background,0);
		}
		
		private var currentClickY:int;
		private var selectItem:ListItemRender;
		private var rect:Rectangle=new Rectangle();
		private function selectedHandle(event:ListItemEvent):void{
			if(selectItem){
				hideHandle(null);
				return;
			}
			
			selectItem=innerList.selectedItem;
			
			var mx:int=selectItem.x+(selectItem.width>>1);
			var my:int=selectItem.y+selectItem.height+20;
			
			if(_bmd.rect.contains(mx,my)==false || my==0)return;
			currentClickY=my;
			
			var time:int=getTimer();
			
			rect.setEmpty();
			rect.width=_bmd.width;
			rect.height=my;
			
			var empty:Point=new Point();
			var bmd:BitmapData=new BitmapData(rect.width,rect.height,true,0);
			bmd.copyPixels(_bmd,rect,empty);
			
			_cache_top.y=0;
			_cache_top.texture=Texture.fromBitmapData(bmd,false);
			_cache_top.readjustSize();
			
			
			rect.y=my;
			rect.height=_bmd.height-my;
			bmd=new BitmapData(rect.width,rect.height,true,0);
			bmd.copyPixels(_bmd,rect,empty);
			
			_cache_botton.y=my;
			_cache_botton.texture=Texture.fromBitmapData(bmd,false);
			_cache_botton.readjustSize();
			
			_topList.y=my;
			
			var tw:Tween;
			
			expadHeight=Math.random()*100+250;
			
			var bottonPos:int=expadHeight;
			
			var maxBottonPos:int=_bmd.height-100;
			var _currentExpadHeight:int=bottonPos;
			
			if(bottonPos+my>maxBottonPos){
				var topPos:int=maxBottonPos-bottonPos-my;
				tw=new Tween(_cache_top,expadTime);
				tw.onUpdate=topOnUpdate;
				tw.animate("y",topPos);
				Starling.juggler.add(tw);
				
				bottonPos=maxBottonPos-my;
			}
			
			layout.expandBy(selectItem,topPos,bottonPos,0.1,false);
			
			_topList.show(_bmd.width,expadHeight,mx);
			
			
			tw=new Tween(_cache_botton,expadTime);
			tw.animate("y",my+bottonPos);
			Starling.juggler.add(tw);
			
			this.addChildAt(_cache_top,0);
			this.addChildAt(_topList,1);
			this.addChildAt(_cache_botton,2);
			
			
			this.removeChild(_background);
			
			trace(getTimer()-time);
		}
		
		private function topOnUpdate():void{
			_topList.y=_cache_top.y+_cache_top.height;
		}
		
		private function hideHandle(event:TouchEvent):void{
			if(selectItem==null){
				return;
			}
			if(event){
				var touch:Touch=event.getTouch(stage);
				if(touch.phase !=TouchPhase.ENDED){
					return;
				}
			}
			
			var tw:Tween=new Tween(_cache_botton,expadTime);
			tw.addEventListener(Event.REMOVE_FROM_JUGGLER, hideComplete);
			
			tw.animate("y",currentClickY);
			Starling.juggler.add(tw);
			
			tw=new Tween(_cache_top,expadTime);
			tw.onUpdate=topOnUpdate;
			tw.animate("y",0);
			Starling.juggler.add(tw);
			
			layout.expandBy(selectItem,0,0,1,true);
		}
		
		public function displayList(value:*):void{
			innerList.displayList(value);
		}
		
		
		private function hideComplete(event:Event):void{
			selectItem=null;
			
			innerList.selectedIndex=-1;
			
			this.removeChild(_cache_top);
			this.removeChild(_cache_botton);
			this.removeChild(_topList);
			
			this.addChildAt(_background,0);
		}
	}
}